package com.enterpriseDigitalPlatform.controller.renshi.moneyMonth;

import com.enterpriseDigitalPlatform.dto.PageRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class MoneyMonthDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public MoneyMonthDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int editData(MoneyMonthDTO moneyMonthDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (moneyMonthDTO.getRequiredDays() != null) {
            keyArr.add("required_days = ?");
            valueArr.add(moneyMonthDTO.getRequiredDays());
        }
        if (moneyMonthDTO.getActualDays() != null) {
            keyArr.add("actual_days = ?");
            valueArr.add(moneyMonthDTO.getActualDays());
        }
        if (moneyMonthDTO.getMoneyBase() != null) {
            keyArr.add("money_base = ?");
            valueArr.add(moneyMonthDTO.getMoneyBase());
        }
        if (moneyMonthDTO.getMoneyJixiao() != null) {
            keyArr.add("money_jixiao = ?");
            valueArr.add(moneyMonthDTO.getMoneyJixiao());
        }
        if (moneyMonthDTO.getMoneyBuzhu() != null) {
            keyArr.add("money_buzhu = ?");
            valueArr.add(moneyMonthDTO.getMoneyBuzhu());
        }
        if (moneyMonthDTO.getMoneyDeduct() != null) {
            keyArr.add("money_deduct = ?");
            valueArr.add(moneyMonthDTO.getMoneyDeduct());
        }
        if (moneyMonthDTO.getMoneyPayment() != null) {
            keyArr.add("money_payment = ?");
            valueArr.add(moneyMonthDTO.getMoneyPayment());
        }
        if (moneyMonthDTO.getMoneyTax() != null) {
            keyArr.add("money_tax = ?");
            valueArr.add(moneyMonthDTO.getMoneyTax());
        }
        if (moneyMonthDTO.getMoneyJixiaoActual() != null) {
            keyArr.add("money_jixiao_actual = ?");
            valueArr.add(moneyMonthDTO.getMoneyJixiaoActual());
        }
        if (moneyMonthDTO.getMoneyBaseActual() != null) {
            keyArr.add("money_base_actual = ?");
            valueArr.add(moneyMonthDTO.getMoneyBaseActual());
        }
        if (moneyMonthDTO.getMoneyActual() != null) {
            keyArr.add("money_actual = ?");
            valueArr.add(moneyMonthDTO.getMoneyActual());
        }
        String sql = "update renshi_money_month set " + String.join(",", keyArr) + " where id = ?";
        valueArr.add(moneyMonthDTO.getId());
        return jdbcTemplate.update(sql, valueArr.toArray());
    }


    public PageRes<MoneyMonth> getList(MoneyMonthDTO moneyMonthDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (moneyMonthDTO.getId() != null) {
            keyArr.add("id = ?");
            valueArr.add(moneyMonthDTO.getId());
        }
        if (moneyMonthDTO.getStaffId() != null) {
            keyArr.add("staff_id = ?");
            valueArr.add(moneyMonthDTO.getStaffId());
        }
        if (moneyMonthDTO.getDeptId() != null) {
            keyArr.add("dept_id = ?");
            valueArr.add(moneyMonthDTO.getDeptId());
        }
        if (moneyMonthDTO.getWorkMonth() != null) {
            keyArr.add("work_month = ?");
            valueArr.add(moneyMonthDTO.getWorkMonth());
        }
        if (moneyMonthDTO.getWorkMonthStart() != null && moneyMonthDTO.getWorkMonthEnd() != null) {
            keyArr.add("work_month between ? and ?");
            valueArr.add(moneyMonthDTO.getWorkMonthStart());
            valueArr.add(moneyMonthDTO.getWorkMonthEnd());
        }
        String sql = "select * from renshi_money_month ";
        String sqlCount = "select count(*) from renshi_money_month ";
        if (!keyArr.isEmpty()) {
            sql += " where " + String.join(" and ", keyArr);
            sqlCount += " where " + String.join(" and ", keyArr);
        }
        Long total = jdbcTemplate.queryForObject(sqlCount, Long.class, valueArr.toArray());
        if (moneyMonthDTO.getPage() != null && moneyMonthDTO.getPageSize() != null) {
            sql += " order by id desc limit ?,?";
            valueArr.add((moneyMonthDTO.getPage() - 1) * moneyMonthDTO.getPageSize());
            valueArr.add(moneyMonthDTO.getPageSize());
        }
        List<MoneyMonth> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MoneyMonth.class), valueArr.toArray());
        return new PageRes<>(moneyMonthDTO.getPage(), moneyMonthDTO.getPageSize(), total, list);
    }
}
